package com.hlt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlt.dto.CategoriesPageQueryDTO;
import com.hlt.entity.Categories;
import com.hlt.mapper.CategoriesMapper;
import com.hlt.result.PageResult;
import com.hlt.service.ICategoriesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzf
 * @since 2024-11-25
 */
@Service
public class CategoriesServiceImpl extends ServiceImpl<CategoriesMapper, Categories> implements ICategoriesService {

    /**
     * 数量加一
     * @param categoryId
     */
    @Override
    public void addCount(Long categoryId) {
        Categories categories = baseMapper.selectById(categoryId);
        categories.setCount(categories.getCount() + 1);
        baseMapper.updateById(categories);
    }

    /**
     * 数量减一
     * @param categoryId
     */
    @Override
    public void subCount(Long categoryId) {
        Categories categories = baseMapper.selectById(categoryId);
        categories.setCount(categories.getCount() - 1);
        baseMapper.updateById(categories);
    }

    /**
     * 分页查询分类
     * @param categoriesPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoriesPageQueryDTO categoriesPageQueryDTO) {
        Page<Categories> page = new Page<>(categoriesPageQueryDTO.getPageNum(), categoriesPageQueryDTO.getPageSize());
        LambdaQueryWrapper<Categories> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(categoriesPageQueryDTO.getCategoryName() != null, Categories::getCategoryName, categoriesPageQueryDTO.getCategoryName());
        page = page(page, queryWrapper);
        return new PageResult(page.getTotal(), page.getRecords());
    }
}
